//
//  TKIMLabel.m
//  TKIM_Thinkive
//
//  Created by liupm on 16/4/12.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMLabel.h"
#import "TKIMHeadView.h"
@implementation TKIMLabel
{
    CAShapeLayer *shape;
    NSArray *arr;
    UIImageView *imageView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _roundImageColor = [UIColor whiteColor];
    }
    return self;
}


-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    UIImage *image = [self getRounderImage:self.bounds];
    if (!imageView) {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self insertSubview:imageView atIndex:0];
    }
    imageView.image = image;
}


-(UIImage *)getRounderImage:(CGRect)rect
{
    CGRect imgRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    UIGraphicsBeginImageContextWithOptions(imgRect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef currnetContext = UIGraphicsGetCurrentContext();
    CGContextAddPath(currnetContext, [UIBezierPath bezierPathWithRoundedRect:imgRect cornerRadius:imgRect.size.width/2].CGPath);
    CGContextClip(currnetContext);
    CGContextSetFillColorWithColor(currnetContext, _roundImageColor.CGColor);
    CGContextFillRect(currnetContext, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


-(void)setLayerViewColorIndex:(NSInteger)index
{
    //禁用隐式动画
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    UIColor *color = [TKIMColorHelper stringToColor:arr[index]];
    self.backgroundColor = color;
    self.highlightedTextColor = color;
    self.highlighted = NO;
    self.opaque =YES;
    [CATransaction commit];
}

@end
